<?php
namespace app\property\controller;
use think\Validate;
use think\Loader;
use app\index\validate\Workrecord;
class Main extends Base{
    public function index(){
        return $this->fetch();
    }
    public function workorder(){
       //获取工种
       $worktypes = db('worktype')->field("id as value,worktype as title")->select();
       $worktypes[] = ['value'=>'0','title'=>'其他'];
       $this->assign("worktypes",json_encode($worktypes));
       $workcontents = db("workcontent")->field("workcontent as title,format(coefficient,2) as value,typeid")->select();
       $res = [];
       foreach ($worktypes as $worktype)
       {
           foreach ($workcontents as $workcontent)
           {
                if ($worktype['value']==$workcontent['typeid']){
                    if (!isset($res[$worktype['value']])){
                        $res[$worktype['value']] = [];
                    }
                    unset($workcontent['typeid']);
                    array_push($res[$worktype['value']],$workcontent);
                }
           }
       }
       $this->assign("workcontents",json_encode($res));
       //当前日期分配给前端
       $date = date("Y/m/d");
       $this->assign("date",$date);
       
      
       
       return $this->fetch();
    }
    /**
     * @author chenj
     * @method 
     */
    public function save_work(){
        try {
            $data = input();
            $data['create_time'] = time();
            $data['userid'] = getSession("user","property_")['userid'];
            $data['username'] = getSession("user","property_")['name'];
            //初始化一个验证器
            $validate = Loader::validate('Workrecord');
            if (!$validate->check($data)){
                //生成新令牌
                $hash = request()->token("hash");
                echo "{\"errcode\":-1003,\"errmsg\":\"".$validate->getError().",请重新提交！\",\"__hash__\":\"".$hash."\"}";
                return;
            }
            //判断录入时间
            $startcheck = db("workrecord")->where("userid = '".$data['userid']."' and date = '".$data['date']."' and starttime <= '".$data['starttime']."' and endtime > '".$data['starttime']."'")->count();
            if ($startcheck>0){
                echo "{\"errcode\":-1004,\"errmsg\":\"开始时间与其他时间段冲突！\"}";
                return ;
            }
            $endcheck = db("workrecord")->where("userid = '".$data['userid']."' and date = '".$data['date']."' and starttime < '".$data['endtime']."' and endtime >= '".$data['endtime']."'")->count();
            if ($endcheck>0){
                echo "{\"errcode\":-1005,\"errmsg\":\"结束时间与其他时间段冲突！\"}";
                return ;
            }
            
            //添加数据
            $res=db("workrecord")->insert($data);
            if ($res){
                echo "{\"errcode\":0,\"errmsg\":\"派工单保存成功！\"}";
                return;
            }else{
                echo "{\"errcode\":-1001,\"errmsg\":\"派工单保存失败!\"}";
                return;
            }
        } catch (\Exception $e) {
            echo "{\"errcode\":-1002,\"errmsg\":\"".$e->getMessage()."\"}";
            return;
        }
        
    }
    
    public function save_rest(){
        try {
            $data = input();
            $data['create_time'] = time();
            $data['userid'] = getSession("user","property_")['userid'];
            $data['username'] = getSession("user","property_")['name'];
            //初始化一个验证器
            $validate = Loader::validate('Restrecord');
            if (!$validate->check($data)){
                //生成新令牌
                $hash = request()->token("hash");
                echo "{\"errcode\":-1003,\"errmsg\":\"".$validate->getError()."\",\"__hash__\":\"".$hash."\"}";
                return;
            }
            
            //添加数据
            $res=db("restrecord")->insert($data);
            if ($res){
                echo "{\"errcode\":0,\"errmsg\":\"待工单保存成功！\"}";
                return;
            }else{
                echo "{\"errcode\":-1001,\"errmsg\":\"待工单保存失败!\"}";
                return;
            }
        } catch (\Exception $e) {
            echo "{\"errcode\":-1002,\"errmsg\":\"".$e->getMessage()."\"}";
            return;
        }
    }
    
    
    public function welcom(){
        dump(getSession("user",'property_'));
        echo "欢迎来到物业管理系统！";
    }
}